package com.rico.kok.controller.post;

import com.rico.kok.entity.Post;
import com.rico.kok.entity.User;
import com.rico.kok.service.interf.PostService;
import com.rico.kok.tool.MessageUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class DeleteController {

    @Autowired
    PostService postService;

    @Autowired
    MessageUtils messageUtils;

    @RequiresRoles("user")
    @RequestMapping("/deletePost.htm")
    public String deletePost(String id, Model model, HttpSession session) {
        Post post = new Post();
        int userId=((User)session.getAttribute("user")).getUserId();
        int i;
        try {
            i = Integer.parseInt(id);
            post.setPostId(i);
            post.setUserId(userId);
        } catch (Exception e) {
            System.out.println("不合法ID");
            messageUtils.setMsg(model, "错误", "不合法ID", "error");
            return "msg";
        }
        post.setIsDelete((byte) 1);
        postService.deletePost(post);
        String path="/browseUser.htm?id="+userId;
        model.addAttribute("url",path);
        return "forbidBack";
    }
}
